import os
from PyQt5.QtWidgets import (
    QApplication,
    QWidget,
    QFileDialog,  # Диалог открытия файлов (и папок)
    QLabel,
    QPushButton,
    QListWidget,
    QHBoxLayout,
    QVBoxLayout,
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap
from PIL import Image, ImageFilter

app = QApplication([])
win = QWidget()
win.resize(700, 600)
win.setWindowTitle("Easy Editor")

btn_dir = QPushButton("Папка")
lb_image = QLabel("Картинка")
list_files = QListWidget()

btn_left = QPushButton("Лево")
btn_right = QPushButton("Право")
btn_flip = QPushButton("Зеркало")
btn_sharp = QPushButton("Резкость")
btn_bw = QPushButton("Ч/Б")

col1 = QVBoxLayout()
col1.addWidget(btn_dir)
col1.addWidget(list_files)

col2 = QVBoxLayout()
col2.addWidget(lb_image)
row_tools = QHBoxLayout()
row_tools.addWidget(btn_left)
row_tools.addWidget(btn_right)
row_tools.addWidget(btn_flip)
row_tools.addWidget(btn_sharp)
row_tools.addWidget(btn_bw)
col2.addLayout(row_tools)

row = QHBoxLayout()
row.addLayout(col1, 20)
row.addLayout(col2, 80)
win.setLayout(row)

workdir = ""


def chooseWorkdir():
    global workdir
    workdir = QFileDialog.getExistingDirectory()


def filter(files, extensions):
    result = []
    for filename in files:
        for ext in extensions:
            if filename.endswith(ext):
                result.append(filename)
    return result


def showFilenameList():
    extensions = [".jpg", ".jpeg", ".png", ".gif", ".bmp"]
    chooseWorkdir()
    filenames = filter(os.listdir(workdir), extensions)
    list_files.clear()
    for filename in filenames:
        list_files.addItem(filename)


btn_dir.clicked.connect(showFilenameList)


class ImageProcessor:
    def __init__(self):
        self.image = None
        self.dir = None
        self.filename = None
        self.save_dir = "Modified/"

    def loadImage(self, dir, filename):
        self.dir = dir
        self.filename = filename
        image_path = os.path.join(dir, filename)
        self.image = Image.open(image_path)

    def showImage(self, path):
        lb_image.hide()
        pixmapimage = QPixmap(path)
        w, h = lb_image.width(), lb_image.height()
        pixmapimage = pixmapimage.scaled(w, h, Qt.KeepAspectRatio)
        lb_image.setPixmap(pixmapimage)
        lb_image.show()


win.show()
app.exec_()
